﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ACLibs.Config
{
    /// <summary>
    /// 应用程序默认配置文件操作类
    /// </summary>
    public class DefaultConfig
    {
        #region 变量定义

        /// <summary>
        /// 配置对象实体
        /// </summary>
        private static Configuration config = null;

        #endregion

        #region 数据库连接字符串
        /// <summary>
        /// 设置连接字符串
        /// </summary>
        /// <param name="name">配置名称</param>
        /// <param name="connString">连接字符串</param>
        public static void SetConnString(string name, string connString)
        {
            config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            config.ConnectionStrings.ConnectionStrings.Remove(name);
            config.ConnectionStrings.ConnectionStrings.Add(new ConnectionStringSettings(name, connString));
            config.Save();
        }

        /// <summary>
        /// 设置连接字符串
        /// </summary>
        /// <param name="name">配置名称</param>
        /// <param name="connString">连接字符串</param>
        /// <param name="providerName">提供程序名称</param>
        public static void SetConnString(string name, string connString, string providerName)
        {
            config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            config.ConnectionStrings.ConnectionStrings.Remove(name);
            config.ConnectionStrings.ConnectionStrings.Add(new ConnectionStringSettings(name, connString, providerName));
            config.Save();
        }

        /// <summary>
        /// 设置连接字符串
        /// </summary>
        /// <param name="name">配置名称</param>
        /// <param name="server">服务器</param>
        /// <param name="database">数据库名称</param>
        /// <param name="userid">用户名</param>
        /// <param name="password">密码</param>
        /// <param name="providerName">提供程序名称</param>
        public static void SetConnString(string name, string server, string database, string userid, string password, string providerName)
        {
            DbConnectionStringBuilder builder = new DbConnectionStringBuilder();
            builder.Add("Data Source", server);
            builder.Add("Initial Catalog", database);
            builder.Add("User ID", userid);
            builder.Add("Password", password);
            SetConnString(name, builder.ConnectionString, providerName);
        }

        /// <summary>
        /// 获取指定名称的连接字符串
        /// </summary>
        /// <param name="name">配置名称</param>
        /// <returns>String</returns>
        public static string GetConnString(string name)
        {
            config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            return config.ConnectionStrings.ConnectionStrings[name].ConnectionString;
        }

        /// <summary>
        /// 获取指定名称的连接字符串的指定项
        /// </summary>
        /// <param name="name">配置名称</param>
        /// <param name="keyword">项名称</param>
        /// <returns></returns>
        public static string GetConnString(string name, string keyword)
        {
            DbConnectionStringBuilder builder = new DbConnectionStringBuilder();
            builder.ConnectionString = GetConnString(name);
            if (builder.TryGetValue(keyword, out object value))
            {
                return value.ToString();
            }
            else
            {
                return "";
            }
        }

        /// <summary>
        /// 获取提供程序程序集名称
        /// </summary>
        /// <param name="name">配置名称</param>
        /// <returns></returns>
        public static string GetConnProviderName(string name)
        {
            config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            var x = config.ConnectionStrings.ConnectionStrings[name];
            if (x == null)
            {
                return "";
            }
            else
            {
                return x.ProviderName;
            }
        }
        #endregion

        /// <summary>
        /// 设置KeyValue数据到配置文件
        /// </summary>
        /// <param name="ID">Key</param>
        /// <param name="Value">Value</param>
        public static void SetConfig(string ID, string Value)
        {
            config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            if (config.AppSettings.Settings.AllKeys.ToList().Contains(ID))
            {
                config.AppSettings.Settings[ID].Value = Value;
            }
            else
            {
                config.AppSettings.Settings.Add(ID, Value);
            }
            Save();
        }

        /// <summary>
        /// 清空配置文件
        /// </summary>
        /// <param name="KeyStartWith">指定删除以当前字符开头的项</param>
        public static void ClearData(string KeyStartWith)
        {
            config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            foreach (KeyValueConfigurationElement item in config.AppSettings.Settings)
            {
                if (item.Key.StartsWith(KeyStartWith))
                {
                    config.AppSettings.Settings.Remove(item.Key);
                }
            }
            Save();
        }

        /// <summary>
        /// 获取指定Key配置文件中的数据
        /// </summary>
        /// <param name="ID">Key</param>
        /// <returns></returns>
        public static string GetConfig(string ID)
        {
            config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            if (string.IsNullOrWhiteSpace(config.AppSettings.Settings[ID].Value))
            {
                return "";
            }
            else
            {
                return config.AppSettings.Settings[ID].Value;
            }
        }

        /// <summary>
        /// 保存配置文件
        /// </summary>
        /// <param name="saveMode">保存模式</param>
        /// <returns></returns>
        public static void Save(ConfigurationSaveMode saveMode = ConfigurationSaveMode.Full)
        {
            config.Save(saveMode);
        }
    }
}
